package com.bear.fastrepair.mvvm.ui.employee.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import com.bear.fastrepair.base.BaseDialog
import com.bear.fastrepair.databinding.DialogBillLogBinding
import com.bear.fastrepair.mvvm.ui.employee.adapter.BillAdapter
import com.bear.fastrepair.mvvm.ui.employee.EmployeeViewModel
import com.bear.fastrepair.mvvm.ui.employee.fragment.OrderDetailFragment
import com.blankj.utilcode.util.ToastUtils

class BillDetailDialog(
    context: Context,
    private val fragment: OrderDetailFragment,
    // 无须全局
    private val viewModel: EmployeeViewModel,
    private val serviceOrderId: String
) : BaseDialog(context) {

    private lateinit var binding: DialogBillLogBinding

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogBillLogBinding.inflate(LayoutInflater.from(context))
        setContentView(binding.root)
//        val id = viewModel.getOrderInfoDetail.value?.data?.id
        if (serviceOrderId.isEmpty()) {
            ToastUtils.showLong("订单id获取异常，请重试")
            dismiss()
            return
        }
        viewModel.getBillInfoById(serviceOrderId)
        viewModel.billDetail.observe(fragment) {
            if (it.code == 0) {
                if (it.data.isEmpty()) {
                    ToastUtils.showLong("无账单信息")
                    dismiss()
                    return@observe
                }
                binding.rvBill.let { rv ->
                    rv.layoutManager = LinearLayoutManager(context)
                    rv.adapter = BillAdapter(it.data)
                }
                binding.total.text =
                    "总计收入：${it.data.sumOf { info -> if (info.billType == 0) -info.price else info.price }}"
            } else {
                ToastUtils.showLong("连接服务器异常，请检查网络连接")
                dismiss()
            }
        }
    }
}